<template>
  <el-dialog
    :title="view === 'create'? '新增模块': '编辑模块'"
    :visible="true"
    custom-class="dialogWrapper"
    :before-close="closeDialog">
    <el-form :model="formData" label-width="80px" ref="ruleForm" :rules="rules">
      <el-form-item label="支付id" prop="payMchtId" required>
        <el-input v-model.trim="formData.payMchtId" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="支付私钥" prop="payKey" required>
        <el-input v-model.trim="formData.payKey" auto-complete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="submit('ruleForm')" :disabled="Subabled">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { create, edit } from "@/services/business";
import { getInsFromData} from "@/services/getFromData";
import { RequestError } from "@/services/errors";
import { mapActions } from "vuex";
import editorImage from "@/components/Tinymce/components/editorImage";
var serialize = require('form-serialize');
// import {login} from '@/services/login'
// import menuModules from '@/router/modules.json'
export default {
  name: "edit-module",
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请选择权限'));
      } else {
        callback();
      }
    }
    return {
      menus: [],
      list: [],
      institutionNo:"",
      formData: {
        payMchtId: "",
        payKey: "",
      },
      Subabled:false,
      imgShow:"",
      id:"",
      dialogImageUrl: '',
      dialogVisible: false,
      dialogFormVisible: false,
      menuInfo: [],
      rules: {
        payMchtId: [{ required: true, message: "请输入支付id", trigger: "blur" },
        { min: 1, max: 20, message: '长度在 20个字符以内', trigger: 'blur' }],
        payKey: [{ required: true, message: "请输入支付私钥", trigger: "blur" },
        { min: 1, max: 100, message: '长度在 100个字符以内', trigger: 'blur' }]
      }
    }
  },
  props: {
    view: {
      type: String,
      default: "create"
    }
  },
  methods: {
    ...mapActions(["setUserInfo"]),
    closeDialog() {
      this.$emit("close");
    },
    submit(formName) {
    	this.Subabled = true;
      this.$refs[formName].validate(async valid => {
        if (valid) {
          try {
            if (this.view === "create") {
              await create({
                institutionNo:this.institutionNo,
                payMchtId: this.formData.payMchtId,
                payKey: this.formData.payKey,
              });
            } else {
              await edit({
                id:this.id,
                institutionNo:this.institutionNo,
                payMchtId: this.formData.payMchtId,
                payKey: this.formData.payKey,
              });
            }
            this.dialogFormVisible = false;
            this.$emit("close", true);
            this.$message({
              showClose: true,
              message: "提交成功",
              type: "success"
            });
            this.Subabled = false;
          } catch (e) {
            if (e instanceof RequestError) {
              this.$alert(e.message, "提示", {
                type: "error",
                confirmButtonText: "确定"
              });
              this.Subabled = false;
            } else {
              throw e;
            }
          }
        } else {
          return;
        }
      });
    }
  },
  mounted() {},
  components: {
      'editorImage': editorImage
    }
};
</script>
<style>
.dialogWrapper {
  width: 540px;
}
.el-select {
  width: 100%;
}
.editImg{
	  overflow: hidden;
    background-color: #fff;
    border: 1px solid #c0ccda;
    border-radius: 6px;
    box-sizing: border-box;
    width: 148px;
    height: 148px;
    margin: 0 8px 8px 0;
    display: inline-block;
    float: left;
}
.editImg img{
	width: 100%;
	height: 100%;
}
</style>
